TWB.New_Module({
	///////////////////////////
	name : "Sort",    		 //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////
	
	NotFull : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		var mat=TWB.URL().match(/group=(\d+)/);
		mat=(mat==null)?false:mat[1]==0;
		if(TWB.Modules.Icons.Screen()=="overview_villages" && TWB.Modules.Icons.Mode()=="prod" && mat){
			var i;
			var j;
			var pointer=null;
			var rows=TWB.Xpath("//*[@id='production_table']//tr[@class]",6);
			// Clear village list
			TWB.Modules.Villages.ClearAll();
			for(i=0; i<rows.length; i++){
				if(pointer==null){
					for(j=0; j<rows[i].childNodes.length; j++){
						if(rows[i].childNodes[j].nodeName!="#text" && rows[i].childNodes[j].innerHTML==rows[i].childNodes[j].innerHTML.match(/\d+\/\d+/)){
							pointer=j;
							break;
						}
					}
				}
				var vals=rows[i].childNodes[j].innerHTML.match(/(\d+)\/(\d+)/);
				var R=rows[i].childNodes[3].childNodes[1].childNodes[1].childNodes[1];
				if(vals[1]<vals[2]){
					TWB.Modules.Villages.NewVil({
						id : "TWB-v"+R.parentNode.href.match(/village=(\d+)/)[1],
						label : R.textContent.split(/\s\(\d+\|\d+\)/)[0],
						tooltiptext : R.textContent.match(/\(\d+\|\d+\)\s\w\d+/)[0],
						value : R.textContent
					});
				}
			}
			// Update current vil
			TWB.Modules.Villages.UpdateCurVil();
		}
		else{
			TWB.Processor.start("sortnfull","Modules.Sort.NotFull","*",false);
			if(TWB.URL().match(/group=(\d+)/)){
				TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","prod",true).replace(/group=\d+/,"group=0"));
			}
			else{
				TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","prod",true)+"&group=0");
			}
		}
	},
	Full : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		var mat=TWB.URL().match(/group=(\d+)/);
		mat=(mat==null)?false:mat[1]==0;
		if(TWB.Modules.Icons.Screen()=="overview_villages" && TWB.Modules.Icons.Mode()=="prod" && mat){
			var i;
			var j;
			var pointer=null;
			var rows=TWB.Xpath("//*[@id='production_table']//tr[@class]",6);
			// Clear village list
			TWB.Modules.Villages.ClearAll();
			for(i=0; i<rows.length; i++){
				if(pointer==null){
					for(j=0; j<rows[i].childNodes.length; j++){
						if(rows[i].childNodes[j].nodeName!="#text" && rows[i].childNodes[j].innerHTML==rows[i].childNodes[j].innerHTML.match(/\d+\/\d+/)){
							pointer=j;
							break;
						}
					}
				}
				var vals=rows[i].childNodes[j].innerHTML.match(/(\d+)\/(\d+)/);
				var R=rows[i].childNodes[3].childNodes[1].childNodes[1].childNodes[1];
				if(vals[1]==vals[2]){
					TWB.Modules.Villages.NewVil({
						id : "TWB-v"+R.parentNode.href.match(/village=(\d+)/)[1],
						label : R.textContent.split(/\s\(\d+\|\d+\)/)[0],
						tooltiptext : R.textContent.match(/\(\d+\|\d+\)\s\w\d+/)[0],
						value : R.textContent
					});
				}
			}
			// Update current vil
			TWB.Modules.Villages.UpdateCurVil();
		}
		else{
			TWB.Processor.start("sortnfull","Modules.Sort.NotFull","*",false);
			if(TWB.URL().match(/group=(\d+)/)){
				TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","prod",true).replace(/group=\d+/,"group=0"));
			}
			else{
				TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","prod",true)+"&group=0");
			}
		}
	},
	MaxRes : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		var mat=TWB.URL().match(/group=(\d+)/);
		mat=(mat==null)?false:mat[1]==0;
		if(TWB.Modules.Icons.Screen()=="overview_villages" && TWB.Modules.Icons.Mode()=="prod" && mat){
			var i;
			var j;
			var pointer=null;
			var rows=TWB.Xpath("//*[@id='production_table']//tr[@class]",6);
			// Clear village list
			TWB.Modules.Villages.ClearAll();
			for(i=0; i<rows.length; i++){
				if(pointer==null){
					for(j=0; j<rows[i].childNodes.length; j++){
						if(rows[i].childNodes[j].nodeName!="#text" && rows[i].childNodes[j].innerHTML.match(/class=".+wood"/)){
							pointer=j;
							break;
						}
					}
				}
				var vals=rows[i].innerHTML.match(/class="warn/g);
				var R=rows[i].childNodes[3].childNodes[1].childNodes[1].childNodes[1];
				// All 3 res are full
				if(vals!=null && vals.length==3){
					TWB.Modules.Villages.NewVil({
						id : "TWB-v"+R.parentNode.href.match(/village=(\d+)/)[1],
						label : R.textContent.split(/\s\(\d+\|\d+\)/)[0],
						tooltiptext : R.textContent.match(/\(\d+\|\d+\)\s\w\d+/)[0],
						value : R.textContent
					});
				}
			}
			// Update current vil
			TWB.Modules.Villages.UpdateCurVil();
		}
		else{
			TWB.Processor.start("sortnfull","Modules.Sort.NotFull","*",false);
			if(TWB.URL().match(/group=(\d+)/)){
				TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","prod",true).replace(/group=\d+/,"group=0"));
			}
			else{
				TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","prod",true)+"&group=0");
			}
		}
	},
	Nuke : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		
		var i;
		var ar=[];
		var id;
		var V=TWB.gei("TWB-VillageList");
		var S=TWB.gei("TWB-SandboxList");
		for(i=V.childNodes.length-1; i>0; i--){
			S.appendChild(V.childNodes[i]);
		}
		TWB.Modules.Villages.ClearAll();
		for(i=0; i<S.length; i++){
			id=S[i].getAttribute("id").split("TWB-v")[1];
			try{
				if(TWB._SESSION.UNITS[id].snob>0){
					ar.push(id);
				}
			}catch(e){}
		}
		for(i=0; i>ar.length; i++){
			V.appendChild(TWB.gei("TWB-v"+ar[i]));
		}
		TWB.Modules.Villages.ClearSandBox();
	},
	Continent : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		var i;
		var xy;
		var K=[];
		var cont;
		var list="";
		var V=TWB.gei("TWB-VillageList").childNodes;
		for(i=0; i<V.length; i++){
			xy=V[i].getAttribute("tooltiptext").match(/\d+\|\d+/)[0].split("|");
			cont=(Math.round(xy[1]/100)+" "+Math.round(xy[0]/100)).replace(" ");
			if(typeof K[cont]=="undefined"){
				K[cont]=[];
			}
			K[cont].push(V[i].getAttribute("id"));
		}
		for(cont in K){
			list+=cont+" ";
		}
		var V=TWB.gei("TWB-VillageList");
		var S=TWB.gei("TWB-SandboxList");
		var ask=prompt(TWB.lang("filtercon",[list]));
		if(ask){
			ask=ask.match(/\d+/);
			// Move to sandbox
			for(i=V.childNodes.length-1; i>0; i--){
				S.appendChild(V.childNodes[i]);
			}
			// Clear Village List 
			TWB.Modules.Villages.ClearAll();
			for(cont in K){
				if(ask.indexOf(cont)!=-1){
					// populate
					for(i=0; i<K[cont].length; i++){
						V.appendChild(TWB.gei(K[cont]));
					}
				}
			}
			TWB.Modules.Villages.ClearSandBox();
		}
	},
	Offensive : function(){
		var i;
		var vil;
		var V=TWB.gei("TWB-VillageList");
		var S=TWB.gei("TWB-SandboxList");
		// Move to sandbox
		for(i=V.childNodes.length-1; i>0; i--){
			S.appendChild(V.childNodes[i]);
		}
		// Clear Village List 
		TWB.Modules.Villages.ClearAll();
		for(i=S.childNodes.length-1; i>0; i--){
			vil=S.childNodes[i].getAttribute("id").replace("TWB-v","");
			if(TWB.Modules.Villages.IsOffensive(vil)){
				V.appendChild(S.childNodes[i]);
			}
		}
		TWB.Modules.Villages.ClearSandBox();
	},
	Defensive : function(){
		var i;
		var vil;
		var V=TWB.gei("TWB-VillageList");
		var S=TWB.gei("TWB-SandboxList");
		// Move to sandbox
		for(i=V.childNodes.length-1; i>0; i--){
			S.appendChild(V.childNodes[i]);
		}
		// Clear Village List 
		TWB.Modules.Villages.ClearAll();
		for(i=S.childNodes.length-1; i>0; i--){
			vil=S.childNodes[i].getAttribute("id").replace("TWB-v","");
			if(!TWB.Modules.Villages.IsOffensive(vil)){
				V.appendChild(S.childNodes[i]);
			}
		}
		TWB.Modules.Villages.ClearSandBox();
	},
});